<?php

namespace App\Admin\Controllers;

use App\Models\AdminUser;
use App\Models\Product as Model;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;


class Product extends AdminController
{


//    public function index()
//    {
//        $show = Model::find('609112f6da7d000048001ec2');
//
//        return $show->toJson();
//    }

    public function grid()
    {

        $grid = new Grid(new Model());
        $grid->column('_id');
        $grid->column('name');
        $grid->column('color');
        $grid->column('created_at');

        return $grid;
    }


    public function detail($id)
    {
//        \Log::info(Model::find($id));

        $obj = Model::findOrFail($id);
        $show = new Show($obj);


//        $content = new Content();
//        return $content->body($show);


        $show->field('_id', 'ID');
        $show->divider();
        $show->field('name');
        $show->image('图片')->image();
        $show->field('color');
        $show->show()->using([1=>'是', 0=> '否']);

        $show->user('用户', function($user) {
            $user->name();
            $user->id();
        });
        $show->field('created_at');
        $show->field('updated_at');

        return $show;
    }

    public function form()
    {
        $user = Auth::user();
//        \Log::info($user);
        $show = [
            1 => '是',
            0 => '否',
        ];

        $form = new Form(new Model());
        $form->display('_id');

        $form->text('name')->rules('required|min:3');
        $form->text('color', '颜色');
        $form->select('show', '上架')->options($show);
        $form->multipleImage('image','图片')->removable()->sortable();

//        $form->select('user_id', '用户')
//            ->options(AdminUser::all()->pluck('name','id'))
//            ->rules('required');

        if ($form->isCreating()) {
            $form->hidden('user_id', '创建用户')->default($user->id);
        }


        $form->display('created_at');
        $form->display('updated_at');

        return $form;
    }



}
